package com.fengwk.support.fetch;

import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Vector;
import java.util.concurrent.ConcurrentHashMap;

import com.fengwk.support.util.CollectionUtils;
import com.fengwk.support.util.HttpClientUtils;
import com.fengwk.support.util.JsonUtils;
import com.fengwk.support.util.HttpClientUtils.ResponseEntity;
import com.google.gson.reflect.TypeToken;

public class YlksFetch {

	private static final String URL_API = "http://api.ellabook.cn/rest/api/service";
	
	private static final int STATUS_TRUE = 1;
	
	private static final String REQUEST_FIELD_METHOD = "method";
	private static final String REQUEST_FIELD_CONTENT = "content";
	
	private static final String METHOD_LIST_BOOK_COMMONS = "ella.book.listBookCommons";
	private static final String METHOD_GET_BOOK_BY_CODE = "ella.book.getBookByCode";
	
	private static final String CONTENT_REPLACE = "{{replace}}";
	private static final String CONTENT_DAILY_FTEE = "{\"sourceCode\":\"S201712135AQPKA\",\"pageIndex\":0,\"pageSize\":99999999999}";
	private static final String CONTENT_BOOK_CODE = "{\"bookCode\":\"" + CONTENT_REPLACE + "\"}";
	
	private static final Type TYPE_YLKS_DAILY_FREE = new TypeToken<YlksResult<List<YlksDailyFree>>>() {}.getType();
	private static final Type TYPE_YLKS_YLKS_BOOK = new TypeToken<YlksResult<YlksBook>>() {}.getType();
	
	// 获取每日限免书籍
	public static List<YlksDailyFree> getYlksDailyFree() {
		Map<String, String> params = new HashMap<>();
		params.put(REQUEST_FIELD_METHOD, METHOD_LIST_BOOK_COMMONS);
		params.put(REQUEST_FIELD_CONTENT, CONTENT_DAILY_FTEE);
		return getData(params, TYPE_YLKS_DAILY_FREE);
	}
	
	// 获取书本详情
	public static YlksBook getYlksBook(String bookCode) {
		Map<String, String> params = new HashMap<>();
		params.put(REQUEST_FIELD_METHOD, METHOD_GET_BOOK_BY_CODE);
		params.put(REQUEST_FIELD_CONTENT, CONTENT_BOOK_CODE.replace(CONTENT_REPLACE, bookCode));
		return getData(params, TYPE_YLKS_YLKS_BOOK);
	}
	
	// 获取data对象
	private static <D> D getData(Map<String, String> params, Type type) {
		ResponseEntity re = HttpClientUtils.post(URL_API, params, null);
		YlksResult<D> result = JsonUtils.fromJson(re.getStringContent(), type);
		Integer status = result.getStatus();
		if (status != null && status == STATUS_TRUE)
			return result.getData();
		return null;
	}
	
	public static void main(String[] args) {
		Map<String, List<String>> bookNameOosListMap = new ConcurrentHashMap<>();
		List<YlksDailyFree> ydfList = getYlksDailyFree();
		if (ydfList != null) {
			// 多线程抓取
			ydfList.stream().parallel().forEach(ydf -> {
				YlksBook yb = getYlksBook(ydf.getBookCode());
				String bookName = yb.getBookName();
				List<String> oosList = bookNameOosListMap.get(bookName);
				if (oosList == null) {
					oosList = new Vector<>();
					bookNameOosListMap.put(bookName, oosList);
				}
				List<YlksResource> yrList = yb.getEbBookModeResource();
				oosList.addAll(CollectionUtils.toListWithTypeConvert(yrList, yr -> yr.getOssUrl(), true));
			});
		}
		System.out.println(JsonUtils.toJson(bookNameOosListMap));
	}
	
}
